package com.fingard.dsp.bank.directbank.czcb01;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsHisBalDetail;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

import java.util.Calendar;


/**
 * @ClassName CZCB01Balance
 * @Author Zhengzy
 * @Date 2019/12/18 10:37
 * @Version 1.0
 **/
public class CZCB01Balance extends CZCB01Base implements IBalance {
    @Override
    public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception {
        // 余额返回给ATS的对象
        RetAts7506BalQry retAts = new RetAts7506BalQry(p_req7506.reqHeader);
        retAts.writeStart();
        WriteBankLogLn("进入余额查询处理类");
        if (bankFront.userName == null || bankFront.userName == "") {
            retAts.writeRespCode("-1");
            retAts.writeRespInfo("无客户号节点或未配置客户号！");
            retAts.writeEnd();
            return retAts;
        }
        try {
            // 组装上送报文
            XElement stream = new XElement("stream", "");// 内层明文
            XmlTextWriter outTextWriter = new XmlTextWriter();// 外层报文

            outTextWriter.writeStartDocByDeclaration("<?xml version=\"1.0\" encoding=\"GBK\"?>");
            outTextWriter.writeStartElement("CZBANK");

            stream.addChild(new XElement("action", "BC0101"));
            stream.addChild(new XElement("userName", bankFront.org));
            XElement list = new XElement("list", "");
            list.addAttr("name", "userDataList");

            while (p_req7506.readRecord()) {
                XElement row = new XElement("row", "");// 每一条余额查询请求
                row.addChild(new XElement("accountNo", p_req7506.readedDetail.actNum));
                row.addChild(new XElement("accountSeq", ""));
                list.addChild(row);// 把每个请求组装到请求列标签下
            }
            stream.addChild(list);// 把所有请求组装到stream中，构成明文
            // 对明文报文进行加签加密  对xmlTextWriter进行加签加密
            //String encryptData = stream.加签加密;  // 无前置环境，故加签加密 解密验签  待后续开发

            outTextWriter.writeXElement(stream); // 把明文组装入报文
            outTextWriter.writeElementString("signData", "加签加密内容");// 把密文组装入报文
            outTextWriter.writeEndElement();

            String[] bankResult = sendToBank(outTextWriter.toXmlString());
//            String[] bankResult = new String[]{"", "<?xml version=\"1.0\" encoding=\"GBK\"?>\n" +
//                    "<CZBANK>\n" +
//                    "<stream>\n" +
//                    "<status>0000</status>\n" +
//                    "\t\t<statusText></statusText>\n" +
//                    "\t\t<list>\n" +
//                    "\t\t\t<row>\n" +
//                    "<accountNo></accountNo>\t\n" +
//                    "<accountSeq></accountSeq>\n" +
//                    "<accountName></accountName>\n" +
//                    "<currencyID>1</currencyID>\n" +
//                    "<openBankName>33</openBankName>\n" +
//                    "<lastTranDate>34</lastTranDate>\n" +
//                    "<usableBalance>34</usableBalance>\n" +
//                    "<balance>34</balance>\n" +
//                    "<forzenAmt>34</forzenAmt>\n" +
//                    "</row>\n" +
//                    "\t\t</list>\n" +
//                    "</stream>\n" +
//                    "<signData>密文</signData>\n" +
//                    "</CZBANK>\n"};

            if (bankResult[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(bankResult[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String tmpRetCode = tmpBkReader.getSimpleText("status");// 交易状态
                    String tmpRetInfo = tmpBkReader.getSimpleText("statusText");// 交易状态信息
                    if ("0000".equals(tmpRetCode)) {
                        while (tmpBkReader.read("/CZBANK/stream/list/row")) {
                            RetAtsBalDetail detail = new RetAtsBalDetail();
                            XElement tmpRecord = tmpBkReader.readedXElement;
                            detail.respCode = "0";
                            detail.respInfo = tmpRetInfo;
                            detail.actCur = tmpRecord.getSingleText("currencyID");// 币种
                            detail.currBal = tmpRecord.getSingleText("balance");
                            detail.availBal = tmpRecord.getSingleText("usableBalance");
                            detail.frozenBal = tmpRecord.getSingleText("forzenAmt");
                            retAts.writeRetDetail(detail, p_req7506.isBatch);
                        }
                    } else {
                        RetAtsBalDetail detail = new RetAtsBalDetail();
                        detail.respCode = "-1";
                        detail.respInfo = tmpRetInfo;
                        retAts.writeRetDetail(detail, p_req7506.isBatch);
                    }
                } else {
                    retAts.writeRespCode("-1");
                    retAts.writeRespInfo(tmpSbMsg.toString());
                }
            } else {
                retAts.writeRespCode("-1");
                retAts.writeRespInfo(bankResult[1]);
            }
        } catch (Exception e) {
            retAts.writeRespCode("-1");
            retAts.writeRespInfo(e.getMessage());
        }
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception {
        // 余额返回给ATS的对象
        RetAts7505HisBalQry retAts = new RetAts7505HisBalQry(p_req7505.reqHeader);
        String tmpRespCode = "";
        String tmpRespInfo = "";

        Calendar tmpCalendarFrom = Calendar.getInstance();
        tmpCalendarFrom.setTime(p_req7505.getDateFrom());
        Calendar tmpCalendarTo = Calendar.getInstance();
        tmpCalendarTo.setTime(p_req7505.getDateTo());
        retAts.writeStart();
        WriteBankLogLn("进入余额查询处理类");
        if (bankFront.userName == null || bankFront.userName == "") {
            retAts.writeRespCode("-1");
            retAts.writeRespInfo("无客户号节点或未配置客户号！");
            retAts.writeEnd();
            return retAts;
        }
        try {
            // 组装上送报文
            XElement stream = new XElement("stream", "");// 内层明文
            XmlTextWriter outTextWriter = new XmlTextWriter();// 外层报文

            outTextWriter.writeStartDocByDeclaration("<?xml version=\"1.0\" encoding=\"GBK\"?>");
            outTextWriter.writeStartElement("CZBANK");

            stream.addChild(new XElement("action", "BC0102"));
            stream.addChild(new XElement("userName", bankFront.org));
            stream.addChild(new XElement("accountNo", p_req7505.actNum));
            stream.addChild(new XElement("startDate", Format.Date8Format.format(tmpCalendarFrom.getTime())));
            stream.addChild(new XElement("endDate", Format.Date8Format.format(tmpCalendarTo.getTime())));

            // 对明文报文进行加签加密  对xmlTextWriter进行加签加密
            //String encryptData = stream.加签加密;

            outTextWriter.writeXElement(stream); // 把明文组装入报文
            outTextWriter.writeElementString("signData", "加签加密内容");// 把密文组装入报文
            outTextWriter.writeEndElement();


            // 发送报文到银行
            String[] retStr = sendToBank(outTextWriter.toXmlString());
//            String[] retStr = new String[]{"", "<?xml version=\"1.0\" encoding=\"GBK\"?>\n" +
//                    "<CZBANK>\n" +
//                    "<stream>\n" +
//                    "<status>0000</status>\n" +
//                    "       \t<statusText>历史余额查询成功</statusText>\n" +
//                    "<accountNo>57454</accountNo>\n" +
//                    "<accountNm></accountNm>\n" +
//                    "<mngNode></mngNode>\n" +
//                    "<cryType></cryType>\n" +
//                    "\t\t<list name=\"userDataList\">\n" +
//                    "\t\t\t<row>\n" +
//                    "<date>20190102</date>\n" +
//                    "<usableBalance>5555</usableBalance>\n" +
//                    "<balance>555</balance>\n" +
//                    "<forzenAmt>44</forzenAmt>\n" +
//                    "\t\t\t</row>\n" +
//                    "       \t</list>\n" +
//                    "</stream>\n" +
//                    "<signData>密文</signData>\n" +
//                    "</CZBANK>"};
            if (retStr[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(retStr[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    RetAtsHisBalDetail tmpRetDetail = new RetAtsHisBalDetail();
                    String tmpRetCode = tmpBkReader.getSimpleText("status");// 交易状态
                    String tmpRetInfo = tmpBkReader.getSimpleText("statusText");// 交易状态信息
                    if ("0000".equals(tmpRetCode)) {
                        if (tmpBkReader.read("/CZBANK/stream/list/row")) {
                            XElement tmpRecord = tmpBkReader.readedXElement;
                            tmpRetDetail.actBal = tmpRecord.getSingleText("balance");
                            String date = tmpRecord.getSingleText("date");
                            tmpRetDetail.balDate = date.substring(0, 4) + "-" + date.substring(4, 6) + "-" + date.substring(6, 8);
                            retAts.writeRetDetail(tmpRetDetail);
                        }
                    } else {
                        tmpRespCode = "-1";
                        tmpRespInfo = tmpRetInfo;
                    }
                } else {
                    tmpRespCode = "-1";
                    tmpRespInfo = tmpSbMsg.toString();
                }
            } else {
                tmpRespCode = "-1";
                tmpRespInfo = retStr[1];
            }
        } catch (Exception e) {
            retAts.writeRespCode("-1");
            retAts.writeRespInfo(e.getMessage());
        }
        if (tmpRespCode.equals("-1")) {
            retAts = new RetAts7505HisBalQry(p_req7505.reqHeader);
            retAts.writeStart();//写返回ATS的统一头部信息
            retAts.writeRespCode(tmpRespCode);
            retAts.writeRespInfo(tmpRespInfo);
        } else if (retAts.getWrittenRdCount() == 0) {
            retAts.writeRespCode("0");
            retAts.writeRespInfo("返回结果为空，所查询时间段内不存在历史余额");
        }
        retAts.writeEnd();
        return retAts;
    }
}
